class CircleFadeMaterialProperty {
    constructor(options) {
        this._definitionChanged = new Cesium.Event();
        this._color = Cesium.defaultValue(options.color, new Cesium.Color(1.0, 0.0, 0.0, 1.0));
        // 初始边框宽度（以像素为单位）和纹理尺寸信息
        this._borderWidthInPx = Cesium.defaultValue(options.borderWidthInPx, 10);
        this._textureWidth = Cesium.defaultValue(options.textureWidth, 256);
        this._textureHeight = Cesium.defaultValue(options.textureHeight, 256);
        this.updateMaterial();
    }

    updateMaterial() {
        const materialId = `CircleFadeMaterial_${this._color.toCssColorString()}_${this._borderWidthInPx}`;
        let material = Cesium.Material._materialCache.getMaterial(materialId);
        if (!material) {
            material = {
                fabric: {
                    type: materialId,
                    uniforms: {
                        color: this._color,
                        borderWidthInPx: this._borderWidthInPx,
                        textureWidth: this._textureWidth,
                        textureHeight: this._textureHeight,
                        maxRadius: Math.min(this._textureWidth, this._textureHeight) / 2.0
                    },
                    source: `
                    uniform vec4 color;
                    uniform float borderWidthInPx;
                    uniform float textureWidth;
                    uniform float textureHeight;
                    uniform float maxRadius;

                    czm_material czm_getMaterial(czm_materialInput materialInput){
                        czm_material material = czm_getDefaultMaterial(materialInput);
                        material.diffuse = color.rgb;
                        vec2 pixelSt = vec2(materialInput.st.x * textureWidth, materialInput.st.y * textureHeight);
                        vec2 centerPixel = vec2(textureWidth / 2.0, textureHeight / 2.0);
                        float dis = distance(pixelSt, centerPixel);

                        // 根据边框宽度和最大半径计算新的透明度逻辑，实现从外向内覆盖
                        float edgeFactor = step(maxRadius - borderWidthInPx, dis);
                        material.alpha = color.a * edgeFactor;

                        return material;
                    }
                `
                },
                translucent: function (material) {
                    return true;
                }
            };
            Cesium.Material._materialCache.addMaterial(materialId, material);
        }
        return material;
    }

    get isConstant() {
        return false; // 修改为false，表示属性值可能变化
    }

    get definitionChanged() {
        return this._definitionChanged;
    }

    getType(time) {
        return this.updateMaterial().fabric.type;
    }

    getValue(result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = this._color;
        result.borderWidthInPx = this._borderWidthInPx;
        return result;
    }

    setValue(value) {
        if (Cesium.defined(value.borderWidthInPx)) {
            this._borderWidthInPx = value.borderWidthInPx;
            this.updateMaterial();
            this._definitionChanged.raiseEvent(this);
        }
    }

    equals(other) {
        return (this === other ||
            (other instanceof CircleFadeMaterialProperty &&
                Cesium.Property.equals(this._color, other._color) &&
                Cesium.Property.equals(this._borderWidthInPx, other._borderWidthInPx))
        );
    }
}

Object.defineProperties(CircleFadeMaterialProperty.prototype, {
    color: Cesium.createPropertyDescriptor('color'),
    borderWidth: {
        get: function () {
            return this._borderWidthInPx;
        },
        set: function (value) {
            this.setValue({ borderWidthInPx: value });
        }
    }
});





export const initCircleFadeMaterialProperty = ()=>{
    Cesium.CircleFadeMaterialProperty = CircleFadeMaterialProperty;
    Cesium.Material.CircleFadeMaterialProperty = 'CircleFadeMaterialProperty';
}
